package jp.ac.u_tokyo.dch.util.common;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import jp.ac.u_tokyo.dch.core.db.DBConnection;
import jp.ac.u_tokyo.dch.core.exceptions.DBException;
import jp.ac.u_tokyo.dch.dao.common.PsiDao;
import jp.ac.u_tokyo.dch.model.db.DataDetail;
import jp.ac.u_tokyo.dch.model.db.DataItem;
import jp.ac.u_tokyo.dch.model.db.DataSchemaDetail;
import jp.ac.u_tokyo.dch.view.Constants;

import org.apache.commons.lang.xwork.StringUtils;

public class DataInputUtil {

	private static String NEW_LINE = "\r\n";
	/**
	 *
	 * @param inputType
	 * @param freeKbn
	 * @param datas
	 * @return
	 */
	public static String getDataType(String inputType, String freeKbn, String tmSign, String[] datas) {

		if (Constants.INPUT_TYPE_FIELDSELECT.equals(inputType)) {
			return Constants.DATA_TYPE_FIELDSELECT;
		}

		if (datas == null || datas.length == 0) {

			return Constants.DATA_TYPE_NOT_INPUT;
		}

		if (StringUtils.isEmpty(datas[0])) {
			return Constants.DATA_TYPE_NOT_INPUT;
		}

		//TMサインの場合
		if (Constants.TM_SIGN_GAITO.equals(tmSign)) {
			return Constants.DATA_TYPE_TM_PSI;
		}

		//テキスト、日付の場合テキスト
		if (Constants.INPUT_TYPE_TEXT.equals(inputType)
				|| Constants.INPUT_TYPE_HIZUKE.equals(inputType)) {


			return Constants.DATA_TYPE_TEXT;
		//テキストエリアの場合テキストエリア
		} else if (Constants.INPUT_TYPE_TEXTAREA.equals(inputType)) {

			return Constants.DATA_TYPE_TEXTAREA;

		//ドロップダウンの場合フリー区分が立っていればマルチ、以外はテキスト
		} else if (Constants.INPUT_TYPE_DROPDOWNLIST.equals(inputType)) {

			if (Constants.FREEKBN_KA.equals(freeKbn)) {

				return Constants.DATA_TYPE_MULTY;
			}
			return Constants.DATA_TYPE_ONE;

		//ラジオボタンの場合フリー区分が立っていればフリー入力が検査、以外はテキスト
		} else if (Constants.INPUT_TYPE_RADIO.equals(inputType)) {

			if (Constants.FREEKBN_KA.equals(freeKbn)) {

				for (String data : datas) {

					if (Constants.FREE_IDENTITY.equals(data)) {

						return Constants.DATA_TYPE_ONE_FREE;

					}
				}
			}
			return Constants.DATA_TYPE_ONE;

		} else if (Constants.INPUT_TYPE_CHECKBOX.equals(inputType)) {

			if (Constants.FREEKBN_KA.equals(freeKbn)) {

				for (String data : datas) {

					if (Constants.FREE_IDENTITY.equals(data)) {

						return Constants.DATA_TYPE_MULTY_FREE;

					}
				}
			}

			return Constants.DATA_TYPE_MULTY;

		} else if (Constants.INPUT_TYPE_MULTITEXT.equals(inputType)) {

			return Constants.DATA_TYPE_MULTI_TEXT;
		}

		return null;
	}

	public static void setDataDetailView(DataDetail detail, Map<String, DataSchemaDetail> detailMap, PsiDao psiDao) throws DBException {

		String dataType = detail.getDataType();

		String fieldKey = detail.getFieldKey();

		 List<DataItem> dataItems = detail.getList();

		DataSchemaDetail schemaDetail = detailMap.get(fieldKey);

		int size = dataItems.size();

		if (schemaDetail == null) {

			return;
		}

		String options = schemaDetail.getOptions();

		String[] datas = new String[size];

		if (Constants.DATA_TYPE_TEXT.equals(dataType)) {

			if (size != 0) {
				datas[0] = dataItems.get(0).getDbData();
			}

		} else if (Constants.DATA_TYPE_TEXTAREA.equals(dataType)) {

			if (size != 0) {
				datas[0] = dataItems.get(0).getDbAreaData();
			}

		} else if (Constants.DATA_TYPE_ONE.equals(dataType)) {

			if (size != 0) {
				datas[0] = getNumberKeyByValue(options, dataItems.get(0).getDbData());
			}

		} else if (Constants.DATA_TYPE_ONE_FREE.equals(dataType)) {

			if (size != 0) {
				datas[0] = Constants.FREE_IDENTITY;
			}
			detail.setOtherdata(dataItems.get(0).getDbData());

		} else if (Constants.DATA_TYPE_MULTY.equals(dataType)) {

			int i = 0;

			for (DataItem item : dataItems) {

				datas[i] = getNumberKeyByValue(options, item.getDbData());
				i++;
			}

		} else if (Constants.DATA_TYPE_MULTY_FREE.equals(dataType)) {

			int i = 0;

			for (DataItem item : dataItems) {

				if ("0".equals(item.getSort())) {

					datas[i] = getNumberKeyByValue(options, item.getDbData());

				} else if ("1".equals(item.getSort())) {

					datas[i] = Constants.FREE_IDENTITY;
					detail.setOtherdata(item.getDbData());
				}
				i++;
			}
		} else if (Constants.DATA_TYPE_MULTI_TEXT.equals(dataType)) {

			datas = new String[1];

			StringBuilder text = new StringBuilder();

			int i = 0;

			for (DataItem item : dataItems) {

				text.append(item.getDbData());

				if (i != dataItems.size() - 1) {
					text.append(NEW_LINE);
				}

				i++;
			}
			datas[0] = text.toString();

		} else if (Constants.DATA_TYPE_TM_PSI.equals(dataType)) {

			List<String> data = new ArrayList<String>();
			List<String> dataName = new ArrayList<String>();

			for (DataItem item : dataItems) {

				String itemData = item.getDbData();

				String itemDataName = psiDao.selectPsiName(itemData);

				data.add(itemData);
				dataName.add(itemDataName);
			}

			datas = data.toArray(new String[0]);

			detail.setDataName(dataName.toArray(new String[0]));
		}

		detail.setData(datas);
	}

	public static String getNumberKeyByValue(String options, String value) {

		int i = 0;

		for (String option : options.split(Constants.OPTION_DEL)) {

			if (option.equals(value)) {
				return String.valueOf(i);
			}
			i++;
		}
		return null;
	}

	/**
	 *
	 * @param options
	 * @param key
	 * @return
	 */
	public static String getValueByNumberKey(String options, String key) {

		int i = 0;

		for (String option : options.split(Constants.OPTION_DEL)) {

			if (String.valueOf(i).equals(key)) {
				return option;
			}
			i++;
		}
		return null;
	}

	public static void viewDataToDbData(DBConnection dbconn, List<DataDetail> dataDetailList, Map<String, DataSchemaDetail> detailMap) throws DBException {

		if (dataDetailList == null) {
			return;
		}

		PsiDao psiDao = new PsiDao(dbconn);

		for (DataDetail dataDetail : dataDetailList) {

			List<DataItem> dataItems = new ArrayList<DataItem>();

			String fieldKey = dataDetail.getFieldKey();
			String[] datas = dataDetail.getData();

			DataSchemaDetail schemaDetail = detailMap.get(fieldKey);
			String inputType = schemaDetail.getInputType();
			String optionKbn = schemaDetail.getOptionKbn();
			String tmSign = schemaDetail.getTmSign();

			String dataType = DataInputUtil.getDataType(inputType, optionKbn, tmSign, datas);

			setDataDetail(dataType, dataDetail, schemaDetail, dataItems, psiDao);


			dataDetail.setDataType(dataType);
			dataDetail.setList(dataItems);
		}

	}

	/**
	 *
	 * @param dataType
	 * @param detail
	 * @param schemaDetail
	 * @throws DBException
	 */
	public static void setDataDetail(
			String dataType,
			DataDetail detail,
			DataSchemaDetail schemaDetail,
			List<DataItem> dataItems,
			PsiDao psiDao) throws DBException {

		String otherData = detail.getOtherdata();
		String[] datas = detail.getData();
		String options = schemaDetail.getOptions();

		if (Constants.DATA_TYPE_TEXT.equals(dataType)) {

			if (datas.length != 0) {
				DataItem dataItem = new DataItem();
				dataItem.setDbData(datas[0]);
				dataItem.setSort(Constants.DATA_ITEM_SORT_GENERAL);
				dataItems.add(dataItem);
			}

		} else if (Constants.DATA_TYPE_TEXTAREA.equals(dataType)) {

			if (datas.length != 0) {
				DataItem dataItem = new DataItem();
				dataItem.setDbAreaData(datas[0]);
				dataItem.setSort(Constants.DATA_ITEM_SORT_GENERAL);
				dataItems.add(dataItem);
			}

		} else if (Constants.DATA_TYPE_ONE.equals(dataType)) {

			if (datas.length != 0) {
				DataItem dataItem = new DataItem();
				dataItem.setDbData(DataInputUtil.getValueByNumberKey(options, datas[0]));
				dataItem.setSort(Constants.DATA_ITEM_SORT_GENERAL);
				dataItems.add(dataItem);

			}

		} else if (Constants.DATA_TYPE_ONE_FREE.equals(dataType)) {

			if (datas.length != 0) {
				DataItem dataItem = new DataItem();
				dataItem.setDbData(otherData);
				dataItem.setSort(Constants.DATA_ITEM_SORT_CUSTOM);
				dataItems.add(dataItem);
			}

		} else if (Constants.DATA_TYPE_MULTY.equals(dataType)) {

			if (datas.length != 0) {

				for (String data : datas) {

					if (data != null) {
						DataItem dataItem = new DataItem();
						dataItem.setDbData(DataInputUtil.getValueByNumberKey(options, data));
						dataItem.setSort(Constants.DATA_ITEM_SORT_GENERAL);
						dataItems.add(dataItem);
					}
				}

			}

		} else if (Constants.DATA_TYPE_MULTY_FREE.equals(dataType)) {

			if (datas.length != 0) {

				for (String data : datas) {
					if (data != null && !Constants.FREE_IDENTITY.equals(data)) {
						DataItem dataItem = new DataItem();
						dataItem.setDbData(DataInputUtil.getValueByNumberKey(options, data));
						dataItem.setSort(Constants.DATA_ITEM_SORT_GENERAL);
						dataItems.add(dataItem);

					} else if (data != null && Constants.FREE_IDENTITY.equals(data)){
						DataItem dataItem = new DataItem();
						dataItem.setDbData(otherData);
						dataItem.setSort(Constants.DATA_ITEM_SORT_CUSTOM);
						dataItems.add(dataItem);
					}
				}
			}

		} else if (Constants.DATA_TYPE_MULTI_TEXT.equals(dataType)) {

			if (datas.length != 0) {

				for (String data : datas[0].split(NEW_LINE)) {

					DataItem dataItem = new DataItem();
					dataItem.setDbData(data);
					dataItem.setSort(Constants.DATA_ITEM_SORT_GENERAL);
					dataItems.add(dataItem);
				}
			}
		} else if (Constants.DATA_TYPE_FIELDSELECT.equals(dataType)) {

			DataItem dataItem = new DataItem();
			dataItem.setDbData(options);
			dataItem.setSort(Constants.DATA_ITEM_SORT_GENERAL);
			dataItems.add(dataItem);

		} else if (Constants.DATA_TYPE_TM_PSI.equals(dataType)) {

			String[] dataNames = detail.getDataName();

			int i = 0;

			for (String data : datas) {

				if (data != null) {
					DataItem dataItem = new DataItem();
					dataItem.setDbData(data);
					dataItem.setSort(Constants.DATA_ITEM_SORT_GENERAL);
					dataItems.add(dataItem);

					psiDao.insert(data, dataNames[i]);

					i++;
				}
			}
		}

	}


}
